package socialplaytime;
/* 
 * PlaySet class let teachers be able to add, delete, or update
 * student information. 
*/
import java.util.*;
import java.io.*;

public class PlaySet {

	protected Hashtable<String, String> m_option = null;
	protected char[] modulenum;

	public PlaySet() {
		m_option = new Hashtable<String, String>();
		m_option.put("admin", "9");
		buildPlay();
	}
	protected char[] getModulenum(String name) {
		if(searchName(name)) {
			char[] modulenum = m_option.get(name).toCharArray();
			return modulenum;
		}
		return null;
	}

	protected void buildPlay() {  // read from text file and put the information into hashtable
		String thisLine;
		try {  
			BufferedReader br = new BufferedReader(new FileReader("students.txt"));
			while ((thisLine = br.readLine()) != null) { 
				String[] temp;
				String delimiter = ",", str = "";
				temp = thisLine.split(delimiter);
				int i = 1;
				str = temp[i++];
				while(i< temp.length)
					str += temp[i++];
				m_option.put(temp[0].toLowerCase(), str); // hashtable key is name
			}
			br.close();
		} catch (IOException e) {
			System.err.println("Error: " + e);
		}
	}
	
	
	protected boolean checkCondition(String ch) {
		boolean ans = false;
		if    (ch.equals("9") || ch.equals("1") || ch.equals("2") || ch.equals("3") || ch.equals("4") 
				|| ch.equals("1 2") || ch.equals("1 3") || ch.equals("1 4") 
				|| ch.equals("2 3") || ch.equals("2 4") || ch.equals("3 4") 
				|| ch.equals("1 2 3") || ch.equals("1 2 4") || ch.equals("1 3 4")
				|| ch.equals("2 3 4") || ch.equals("1 2 3 4")){				
			ans = true;			
		}
		return ans;
	}
	
	protected boolean searchName(String name){
		boolean search = false;
		name = name.toLowerCase();
		if(m_option.containsKey(name)){
			search = true;
		}
		return search;
	}
	
	protected String add(String str1, String str2){
		if(searchName(str1)) { // existing same name or already the person is on the list
			return ( str1 + " is already in the list! " +
					"\nAdd new student only!!!");				
		}
		else { // the student's name is not on the students list
			if(checkCondition(str2)){
				m_option.put(str1, str2);
				return "success";			
			}
			else // Invalid choices
				return ( "Invalid choice!!\nYou can enter only 1 thru 4 separated with a space");
		}	
	}
	
	protected String update(String str1, String str2) {
		if(searchName(str1)) { // found the name on the list
			if(checkCondition(str2)) { // need to update student's choices
				m_option.remove(str1); // remove matched name from the hashtable for update
				m_option.put(str1, str2); // put matched name and new choice to the hashtable
				return ("success");	
			}
			else  // Invalid choices
				return ( "Invalid choice!!\nYou can enter only 1 thru 4 separated with a space");		
		   
		}
		else  // cannot update : not existing name
			return ( str1 + " is not on the list to update!");			
	}
	
	protected String delete(String str1, String str2) {
		if(searchName(str1)) { // found the name on the list
			m_option.remove(str1); // remove matched name from the hashtable for update
			return ("success");	
		}
		else  // cannot delete : not existing name		
			return ( str1 + " is not on the list to delete!");	
	}
	
	protected void resetPlay() {
		try{  // Write back to the new information to the file 
			BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));
			ArrayList<String> list = new ArrayList<String>(m_option.keySet()); 
			Collections.sort(list); 
			for (String key : list) { 
				char[] choice = m_option.get(key).toCharArray();
				int index = key.indexOf(" ");
				key = key.substring(0,1).toUpperCase() + key.substring(1);
				if(index>0)
					key = key.substring(0, index+1) + key.substring(index+1, index+2).toUpperCase() + key.substring(index+2);
				
				String str1 = "";
				int i = 0;
				do {
					str1 += ",";
					str1 += choice[i++];
				}while(i< choice.length);
				String Line = key + str1;
				bw.write(Line);
				bw.newLine();
			} 
			bw.close();  //Close the output stream
		}catch (Exception e) { //Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
}
